<?php
require_once '../include/config.php';
require_once '../include/utils.class.php';
require_once '../include/orm.class.php';

$__sys = Utils::get_param('__sys', NULL);
$__inst = Utils::get_param('__inst', NULL);

$session_id = Utils::get_param('PHPSESSID', NULL);

if ($session_id) {
  session_id($session_id);
  session_start();
} else {
  session_name(SESSION);
  session_start();
}

if (!isset($_SESSION[SYS . $__sys])) {
  die('Session Not Found');
}

$app_data = $_SESSION[SYS . $__sys];
$inst = $app_data->instances[$__inst];

if (!in_array($inst->__folder, $app_data->plugins)) {
  die('Acceess Denied');
}

if ($app_data->sys->login_required == 1 && (!(isset($app_data->is_authorised)) || $app_data->is_authorised == 0) && !in_array($inst->__folder, $app_data->exceptions)) {
  die('Not Authenticated');
}

$orm = new ORM();

$class_id = Utils::get_param('id', 0);
$start = Utils::get_param('start', date('Y-m-d', strtotime('last monday', strtotime('tomorrow'))));
$end = date('Y-m-d', strtotime($start) + (86400 * 4));

$opts = new stdClass();
$opts->cd = 't.id = :v1';
$opts->cdv = array(':v1' => $class_id);
$opts->sel = 't.*, t1.start AS __start, t1.end AS __end';
$opts->jt = 'LEFT JOIN pb_colg_intake AS t1 ON t1.id = t.intake_id';
$data = $orm->get('pb_colg_class', 't.course_name', 'ASC', NULL, NULL, $opts)->d;

$rslt = $orm->get('pb_colg_template', NULL, NULL, NULL, NULL, NULL)->d;
if (count($rslt) > 0 && file_exists($rslt[0]->c_attendance)) {
  set_time_limit(0);
  /** Include path **/
  set_include_path(get_include_path() . PATH_SEPARATOR . '../3rd_party/phpexcel/');
  /** PHPExcel_IOFactory */
  include 'PHPExcel/IOFactory.php';
  
  foreach ($data as $idx => $itm) {
    output_class($itm, $rslt[0]->c_attendance);
  }
};

function output_class($obj, $tmpl) {
  global $orm, $start, $end;
  
  $opts = new stdClass();
  $opts->cd = 't.class_id = :v1 AND (t2.start <= :v2 AND t2.end >= :v2) AND t.status != 3';
  $opts->cdv = array(':v1' => $obj->id, ':v2' => $start);
  $opts->sel = 't.*, t1.id_number AS __id_number, t1.first_name AS __first_name, t1.last_name AS __last_name, t1.preferred_name AS __preferred_name, t1.citizenship AS __citizenship';
  $opts->jt = 'LEFT JOIN pb_colg_student AS t1 ON t1.id = t.student_id LEFT JOIN pb_colg_enrolled_intake AS t2 ON t2.id = t.enrolled_intake_id';
  $rslt = $orm->get('pb_colg_enrolled_class', 't1.id_number', 'ASC', NULL, NULL, $opts)->d;
  
  if (count($rslt) > 0) {
    $objReader = PHPExcel_IOFactory::createReader('Excel5');
    $objPHPExcel = $objReader->load($tmpl);
    
    if ($obj->teacher_id == 0) {
      $objPHPExcel->getActiveSheet()->setCellValue('H1', '');
    }
    $objPHPExcel->getActiveSheet()->setCellValue('J1', (($obj->teacher_id == 0) ? '' : $obj->teacher_name));
    $objPHPExcel->getActiveSheet()->setCellValue('J2', trim($obj->course_name . ' ' . $obj->name));
    $objPHPExcel->getActiveSheet()->setCellValue('J3', date('d M y', strtotime($start)) . ' to ' . date('d M y', strtotime($end)));
    
    $obj->__times = array();
    
    $obj->times = json_decode($obj->times);
    foreach ($obj->times as $itm) {
      if ($obj->type == PT_LANGUAGE) {
        $tmp = explode('-', $itm);
        $obj->__times[] = 'MON ' . $tmp[0];
        $obj->__times[] = 'TUE ' . $tmp[0];
        $obj->__times[] = 'WED ' . $tmp[0];
        $obj->__times[] = 'THU ' . $tmp[0];
        $obj->__times[] = 'FRI ' . $tmp[0];
      } else {
        $tmp = explode('#', $itm);
        $tmp2 = explode(' ', $tmp[0]);
        $obj->__times[] = date('D', strtotime($tmp[0])) . ' ' . $tmp2[1];
      }
    }
    
    $style = array(
      'borders' => array(
        'outline' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array('argb' => '00000000'),
        ),
      ),
      'font'  => array(
        'bold'  => true
      )
    );
    $style2 = array(
      'borders' => array(
        'outline' => array(
          'style' => PHPExcel_Style_Border::BORDER_THIN,
          'color' => array('argb' => '00000000'),
        ),
      ),
    );
    
    $time_start = 72;
    foreach ($obj->__times as $idx => $itm) {
      $objPHPExcel->getActiveSheet()->setCellValue(chr($time_start + $idx) . '7', $itm);
      $objPHPExcel->getActiveSheet()->getStyle(chr($time_start + $idx) . '7')->applyFromArray($style);
    }
    
    $student_start = 8;
    foreach ($rslt as $idx => $itm) {
      $itm->__start = date('d M y', strtotime($itm->start));
      $itm->__end = date('d M y', strtotime($itm->end));
      
      $objPHPExcel->getActiveSheet()->setCellValue('A' . ($idx + $student_start), $itm->__id_number . (($itm->type_ncea__ != '') ? ' (N)' : ''));
      $objPHPExcel->getActiveSheet()->setCellValue('B' . ($idx + $student_start), $itm->__first_name);
      $objPHPExcel->getActiveSheet()->setCellValue('C' . ($idx + $student_start), $itm->__last_name);
      $objPHPExcel->getActiveSheet()->setCellValue('D' . ($idx + $student_start), $itm->__preferred_name);
      $objPHPExcel->getActiveSheet()->setCellValue('E' . ($idx + $student_start), $itm->__citizenship);
      $objPHPExcel->getActiveSheet()->setCellValue('F' . ($idx + $student_start), $itm->__start);
      $objPHPExcel->getActiveSheet()->setCellValue('G' . ($idx + $student_start), $itm->__end);
      
      $objPHPExcel->getActiveSheet()->getStyle('A' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('B' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('C' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('D' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('E' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('F' . ($idx + $student_start))->applyFromArray($style2);
      $objPHPExcel->getActiveSheet()->getStyle('G' . ($idx + $student_start))->applyFromArray($style2);
      
      $objPHPExcel->getActiveSheet()->getStyle('A' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('B' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('C' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('D' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('E' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('F' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      $objPHPExcel->getActiveSheet()->getStyle('G' . ($idx + $student_start))->getAlignment()->setShrinkToFit(true);
      
      foreach ($obj->__times as $idx2 => $itm2) {
        $objPHPExcel->getActiveSheet()->getStyle(chr($time_start + $idx2) . ($idx + $student_start))->applyFromArray($style2);
      }
    }
    
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    
    // We'll be outputting an excel file
    header('Content-type: application/vnd.ms-excel');

    // It will be called file.xls
    header('Content-Disposition: attachment; filename="' . trim($obj->course_name . ' ' . $obj->name) . '.xls' . '"');

    // Write file to the browser
    $objWriter->save('php://output');
  }
}
?>